package com.mnb.base.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mnb.base.entity.SysUserRole;
import com.mnb.base.mapper.SysUserRoleMapper;
import com.mnb.common.utils.R;
import com.mnb.common.utils.RSAUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

import com.mnb.base.mapper.SysUserMapper;
import com.mnb.base.entity.SysUser;
import com.mnb.base.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private HttpSession session;

    @Override
    public SysUser selectByUserName(String userName) {
        return sysUserMapper.selectByUserName(userName);
    }

    @Override
    public R getPageList(SysUser sysUser) {
        Page page = PageHelper.startPage(sysUser.getPageNum(), sysUser.getPageSize()).doSelectPage(() -> sysUserMapper.getList(sysUser));
        R result = R.ok(page.getResult());
        result.setTotal(page.getTotal());
        return result;
    }

    @Override
    @Transactional
    public R insert(SysUser sysUser) {
        sysUser.setPassword(RSAUtils.encrypt(sysUser.getPassword()));
        sysUser.setUserId(UUID.randomUUID().toString());
        sysUser.setCreateTime(new Date());
        SysUser user = (SysUser) session.getAttribute("user");
        sysUser.setCreateBy(user.getUserName());
        SysUser count = sysUserMapper.selectByUserName(sysUser.getUserName());
        if (count != null)
        {
            return R.fail("用户名已存在");
        }
        sysUserMapper.insert(sysUser);
        // 新增用户与角色管理
        insertUserRole(user);
        return R.ok();
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(SysUser user)
    {
        Long[] roles = user.getRoleIds();
        if (!StringUtils.isEmpty(roles))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roles)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId().toString());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0)
            {
                userRoleMapper.batchUserRole(list);
            }
        }
    }
    @Override
    @Transactional
    public R udpate(SysUser sysUser) {
        String userId = sysUser.getUserId().toString();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(sysUser);
        sysUserMapper.updateByPrimaryKey(sysUser);
        return R.ok();
    }

    @Override
    public R resetUserPwd(SysUser sysUser) {
        sysUser.setPassword(RSAUtils.encrypt(sysUser.getPassword()));
        sysUserMapper.resetUserPwd(sysUser.getUserId(), sysUser.getPassword());
        return R.ok();
    }

    @Override
    @Transactional
    public R deleteUserByIds(String[] userIds) {
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);
        sysUserMapper.deleteUserByIds(userIds);
        return R.ok();
    }

    @Override
    public R getUserRoleList(SysUser sysUser) {
        List<SysUserRole> list = userRoleMapper.getUserRoleList(sysUser.getUserId());
        return R.ok(list);
    }

    @Override
    public SysUser selectByUserId(String userId) {
        return sysUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public R getList(SysUser sysUser) {
        List<SysUser> list =  sysUserMapper.getList(sysUser);
        return R.ok(list);
    }


}
